package com.jbit.examSystem.service.impl;

import com.jbit.examSystem.dao.AdminDao;
import com.jbit.examSystem.dto.UserDto;
import com.jbit.examSystem.exception.LoginErrorException;
import com.jbit.examSystem.pojo.Administrator;
import com.jbit.examSystem.pojo.Teacher;
import com.jbit.examSystem.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Controller
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public Administrator login(UserDto userDto) throws LoginErrorException {
        String name = userDto.getName();
        String password = userDto.getPassword();

        Administrator admin = adminDao.loginByNameAndPassword(name, password);
        if (admin == null){
            throw new LoginErrorException("登录失败，用户名或密码错误!");
        }
        return admin;
    }

    @Override
    public void setLoginTime(Date loginTime, Integer id, Date lastLoginTime) {
        adminDao.setLoginTime(loginTime,id,lastLoginTime);
    }
}
